/* 
 * Skirta klaviaturos funkcijoms
 */
#include "../../include/types.h"
#include "../../include/const.h"
#include "../../include/busIO.h"
#include "../../include/keyboard.h"
#include "../../include/isr.h"
#include "../../include/monitor.h"
#include "../../include/task.h"
#include "../../include/console.h"

extern u8int cursor_x;
extern u8int cursor_y;
extern u16int *video_memory;

/* Nenaudojamas */
short ascii2[128]= {

    NIL, ESC, '1', '2', '3', '4', '5', '6', '7', '8',
    '9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r',
    't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', NIL,
    'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
    '\'', '`', NIL, '\\', 'z', 'x', 'c', 'v', 'b', 'n',
    'm', ',', '.', '/', NIL, '*', NIL, ' ', NIL, NIL,
    NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
    NIL,NIL, NIL, NIL, '-', 0x004B, NIL, 0x004D, '+', NIL,
    NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL,
    NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL, NIL
};
/* -------- */
short ascii[128] = {
    0x0000,0x011B,0x0231,0x0332,0x0433,0x0534,0x0635,0x0736,0x0837,0x0938,0x0A39,0x0B30,0x0C2D,0x0D3D,0x0E08,0x0F09,
    0x1071,0x1177,0x1265,0x1372,0x1474,0x1579,0x1675,0x1769,0x186F,0x1970,0x1A5B,0x1B5D,'\n'  ,0x1D00,0x1E61,0x1F73,
    0x2064,0x2166,0x2267,0x2368,0x246A,0x256B,0x266C,0x273B,0x2827,0x2960,0x2A00,0x2B5C,0x2C7A,0x2D78,0x2E63,0x2F76,
    0x3062,0x316E,0x326D,0x332C,0x342E,0x352F,0x3600,0x372A,0x3800,0x3920,0x3A00,0x3B00,0x3C00,0x3D00,0x3E00,0x3F00,
    0x4000,0x4100,0x4200,0x4300,0x4400,0x4500,0x4600,0x4700,0x4800,0x4900,0x4A2D,0x004B,0x4C00,0x004D,0x4E2B,0x4F00,
    0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,0x5600,0x8500,0x8600,0x0000,0x0000,0x5B00,0x5C00,0x5D00
};


short with_shift[128] = {
  0x0000,0x001B,0x0221,0x0340,0x0423,0x0524,0x0625,0x075E,0x0826,0x092A,0x0A28,0x0B29,0x0C5F,0x0D2B,0x0E08,'\t'  ,
  0x1051,0x1157,0x1245,0x1352,0x1454,0x1559,0x1655,0x1749,0x184F,0x1950,0x1A7B,0x1B7D,'\n'  ,0x001D,0x1E41,0x1F53,
  0x2044,0x2146,0x2247,0x2348,0x244A,0x254B,0x264C,0x273A,0x2822,0x297E,0x2A00,0x2B7C,0x2C5A,0x2D58,0x2E43,0x2F56,
  0x3042,0x314E,0x324D,0x333C,0x343E,0x353F,0x3600,0x372A,0x0038,0x3920,0x3A00,0x5400,0x5500,0x5600,0x5700,0x5800,
  0x5900,0x5A00,0x5B00,0x5C00,0x5D00,0x4500,0x4600,0x4700,0x4800,0x4900,0x4A2D,0x4B00,0x4C00,0x4D00,0x4E2B,0x4F00,
  0x5000,0x5100,0x5200,0x5300,0x5400,0x5500,0x5600,0x8700,0x8800,0x0000,0x0000,0x5B00,0x5C00,0x5D00
};


short shift;

static u8int scan_key(){
  u8int code = inb(0x60); /* Nuskaitome reiksme */
  outb(0x20,0x20); /* Pranesame kad perskaiteme */
  return code;
}

static void keyboard(reg_t regs){
  
  u8int code;
  u8int keypressed =  scan_key();
  if(!(keypressed & 0x80)){ /* Tikriname ar nuspaudeme, ar atleidome */
    if((keypressed == 42)||(keypressed == 54)){
      shift = 1;
    }
    else{
      if(shift==1){
        keyboard_monitor_put(with_shift[keypressed], shift);
      }
      else{
        keyboard_monitor_put(ascii[keypressed], shift);
      }
    }
  }
  else if((keypressed & 0x80)){
    if((keypressed == 170)||(keypressed == 182)){
      shift = 0;
    }
  }
}

void init_keyboard(){
  shift = 0;
  register_interrupt_handler(IRQ1, &keyboard);
}

u8int getchar(){
  cons_t *tmp = cmd_find(getcurrent());
  tmp->readkey = TRUE;
  u8int X = cursor_x;
  u8int Y = cursor_y;
  while(TRUE){
    if(tmp->task->console == getcurrent()){
      if(Y != cursor_y || X != cursor_x){
        break;
      }
    }
  }
  tmp->readkey = FALSE;
  return video_memory[Y*80 + X];
}
